﻿using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using UtfprLibrary.ComponentModel;
using System.ComponentModel;
using UtfprLibrary.ComponentModel.Security;
using System.Drawing.Design;

namespace UtfprLibrary.Windows.Forms {
    public partial class UtfprGradientBoxBase : Control, IUtfprControl, IUtfprControlProvider {

        #region UtfprControlService (IUtfprControl, IUtfprControlProvider)

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UtfprControlEnabled {
            get { return this.Enabled; }
            set { this.Enabled = value; }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UtfprControlVisible {
            get { return this.Visible; }
            set { this.Visible = value; }
        }

        private IUtfprKeyName _functionOrOperation = null;
        [Category("Security"), Description("If IsFunction is true, loads a Function, otherwise loads an Operation")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprRestrictionEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public IUtfprKeyName FunctionOrOperationRestriction {
            get { return _functionOrOperation; }
            set { _functionOrOperation = value; }
        }

        private UtfprControlBase _utfprControlBase;
        [Browsable(false)]
        public UtfprControlBase UtfprControlBase {
            get {
                if (_utfprControlBase == null) {
                    _utfprControlBase = new UtfprControlBase(this);
                }
                return _utfprControlBase;
            }
        }

        #endregion
        
        private Color _color1;
        public Color Color1 {
            get { return _color1; }
            set { 
                _color1 = value;
                Invalidate();
            }
        }

        private Color _color2;
        public Color Color2 {
            get { return _color2; }
            set { 
                _color2 = value;
                Invalidate();
            }
        }
        private LinearGradientMode _linearGradientMode;
        public LinearGradientMode LinearGradientMode {
            get { return _linearGradientMode; }
            set { 
                _linearGradientMode = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe) {
            base.OnPaint(pe);
            PaintGradientColor(this, Color1, Color2, LinearGradientMode);
        }

        private void PaintGradientColor(Control control, Color color1, Color color2, LinearGradientMode mode) {
            LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, control.Width, control.Height), color1, color2, mode);
            Graphics frmGraphics = control.CreateGraphics();
            frmGraphics.FillRectangle(brush, new RectangleF(0, 0, control.Width, control.Height));
            frmGraphics.Dispose();
        }

    }
}
